package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
	"super/constant"
	"super/global"
	"super/strategy"
)

var curStrategy strategy.Strategy

func main() {
	// var port int
	// flag.IntVar(&port, "p", 8080, "server port")
	// flag.Parse()
	// fmt.Println("port:", port)
	var help = `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	输入db连接mysql;
	输入t2j表转java bean;
	输入insertDoc,insert语句转word文档;
	输入selectDoc,select语句转word文档;
	输入ddlDoc,表名转word文档;
	输入back返回super界面
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
	fmt.Println(help)
	fmt.Print("super>")

	var str string

	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		str = scanner.Text()
		str = strings.TrimSpace(str)
		fmt.Println("==============", str, "==============")

		if str == "" {
			continue
		}

		if str == "exit" {
			os.Exit(0)
		}

		if str == "back" {
			fmt.Println(help)
			fmt.Print("super>")
			global.CurStatus = constant.Super
			continue
		}

		if global.CurStatus == constant.Super {
			stg := getStrategy(str)
			if stg == nil {
				fmt.Println("不认识。。。")
				fmt.Print("super>")
			} else {
				stg.Init()
			}
			curStrategy = stg
		} else {
			curStrategy.Handle(str)
		}
	}
}

var strategyMap = make(map[string]strategy.Strategy)

func initStrategyMap() {
	strategyMap["db"] = strategy.DbStrategy{}
	strategyMap["t2j"] = strategy.T2jStrategy{}
	strategyMap["insertDoc"] = strategy.InsertDocStrategy{}
	strategyMap["selectDoc"] = strategy.SelectDocStrategy{}
	strategyMap["ddlDoc"] = strategy.DdlDocStrategy{}
}

func getStrategy(str string) strategy.Strategy {
	return strategyMap[str]
}

func init() {
	initStrategyMap()
}
